/*
 * StuffManager.java
 *
 * Created on __DATE__, __TIME__
 */

package com.shangpintechnology.sci.machinegenerat;

import javax.swing.JOptionPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import com.shangpintechnology.sci.DAO;

/**
 *
 * @author  __USER__
 */
public class StuffManager extends javax.swing.JFrame {

	/** Creates new form StuffManager */
	public StuffManager() {
		initComponents();
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jButtonDel = new javax.swing.JButton();
		jTextFieldStuff = new javax.swing.JTextField();
		jButtonAdd = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jButtonClose = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();
		jLabel2 = new javax.swing.JLabel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("\u5458\u5de5\u7ba1\u7406");
		setResizable(false);
		DAO dao = new DAO();
		
		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				 dao.getStuffArray2(), new String[] { "id",
						"姓名" }) {
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.String.class };
			boolean[] canEdit = new boolean[] { false, true };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}

			public boolean isCellEditable(int rowIndex, int columnIndex) {
				return canEdit[columnIndex];
			}
		});
		jScrollPane1.setViewportView(jTable1);

		jButtonDel.setText("\u5220\u9664");
		jButtonDel.setToolTipText("\u5220\u9664\u6240\u9009\u5458\u5de5");
		jButtonDel.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonDelActionPerformed(evt);
			}
		});

		jButtonAdd.setText("\u6dfb\u52a0");
		jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonAddActionPerformed(evt);
			}
		});

		jLabel1.setText("\u59d3\u540d");
		jLabel1
				.setToolTipText("\u6b32\u6dfb\u52a0\u7684\u5458\u5de5\u7684\u59d3\u540d");

		jButtonClose.setText("\u5173\u95ed");
		jButtonClose.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonCloseActionPerformed(evt);
			}
		});

		jButton1.setText("\u5237\u65b0");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jLabel2.setText("\u5458\u5de5\u7ba1\u7406");

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout.createSequentialGroup().addContainerGap(
										15, Short.MAX_VALUE).addComponent(
										jScrollPane1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										375,
										javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap())
						.addGroup(
								layout
										.createSequentialGroup()
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				68,
																				68,
																				68)
																		.addComponent(
																				jButtonDel)
																		.addGap(
																				50,
																				50,
																				50)
																		.addComponent(
																				jLabel1))
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addGap(
																				122,
																				122,
																				122)
																		.addComponent(
																				jButtonClose)))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jButton1)
														.addGroup(
																layout
																		.createSequentialGroup()
																		.addComponent(
																				jTextFieldStuff,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				69,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				jButtonAdd)))
										.addContainerGap(65, Short.MAX_VALUE))
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								layout.createSequentialGroup().addContainerGap(
										166, Short.MAX_VALUE).addComponent(
										jLabel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										111,
										javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(123, 123, 123)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(jLabel2)
										.addGap(11, 11, 11)
										.addComponent(
												jScrollPane1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												233,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(36, 36, 36)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jButtonAdd)
														.addComponent(
																jTextFieldStuff,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jButtonDel)
														.addComponent(jLabel1))
										.addGap(18, 18, 18)
										.addGroup(
												layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																jButtonClose)
														.addComponent(jButton1))
										.addContainerGap(40, Short.MAX_VALUE)));
		

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		new StuffManager().setVisible(true);
		dispose();
	}

	private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt) {
		dispose();
	}

	private void jButtonDelActionPerformed(java.awt.event.ActionEvent evt) {
		int i = jTable1.getSelectedRow();
		if (i < 0) {
			JOptionPane.showMessageDialog(this, "请先单击某行进行选择，");
			return;
		} else {
			DAO dao = new DAO();
			TableModel model = jTable1.getModel();
			String id = (String) model.getValueAt(i, 0);
			if (id == null || id == "") {
				JOptionPane.showMessageDialog(this, "数据错误。");

			} else if (dao.removeStuff(id)) {
				JOptionPane.showMessageDialog(this, "删除成功。");

			} else {
				JOptionPane.showMessageDialog(this, "删除失败。");

			}
		}
	}

	private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {
		if (null == this.jTextFieldStuff.getText()
				|| "".equals(this.jTextFieldStuff.getText())) {
			JOptionPane.showMessageDialog(this, "未填写员工姓名");
			return;
		}
		DAO dao = new DAO();
		dao.insertStuff(this.jTextFieldStuff.getText());
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new StuffManager().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButtonAdd;
	private javax.swing.JButton jButtonClose;
	private javax.swing.JButton jButtonDel;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JTextField jTextFieldStuff;
	// End of variables declaration//GEN-END:variables

}